import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

import 'medicine_controller.dart';

class MedicinePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetX<MedicineController>(
        init: MedicineController(),
        builder: (controller) {
          return Scaffold(
              appBar: AppBar(title: Text('小儿常用药')),
              body: MasonryGridView.count(
                  itemCount: controller.medicines.length,
                  crossAxisCount: 5, // 每行显示的item数量
                  mainAxisSpacing: 15,
                  crossAxisSpacing: 15,
                  padding: EdgeInsets.all(15),
                  itemBuilder: (context, index) {
                    return _itemLayout(
                      index,
                      controller,
                    );
                  }));
        });
  }

  _itemLayout(int index, MedicineController controller) {
    final item = controller.medicines[index];
    return InkWell(
        onDoubleTap: () {},
        child: Container(
            color: index ~/ 3 == 0
                ? Colors.greenAccent
                : index ~/ 3 == 1
                    ? Colors.redAccent.withAlpha(180)
                    : index ~/ 3 == 2
                        ? Colors.yellowAccent.withAlpha(180)
                        : Color(0xffE0EEF9),
            padding: EdgeInsets.all(5),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  item.name!,
                  style: TextStyle(
                      color: Colors.blueAccent, fontWeight: FontWeight.bold),
                ),
                SizedBox(
                  height: 3,
                ),
                Text(item.function!),
                SizedBox(
                  height: 3,
                ),
                Text(
                  item.note!,
                  style: TextStyle(color: Colors.redAccent),
                ),
              ],
            )));
  }
}
